package me.firstapp.repository.weixin.order.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.order.KaMiOrder;
import me.firstapp.module.weixin.order.MobileFareOrder;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.weixin.order.MobileFareOrderRepository;

@Repository
public class MobileFareOrderRepositoryImpl extends EntityRepositoryImpl<MobileFareOrder>
		implements MobileFareOrderRepository {

	@Override
	protected Class<MobileFareOrder> getPersistentClass() {
		return MobileFareOrder.class;
	}

	@Override
	public Page<MobileFareOrder> findMobileFareOrderPageByOpenId(Integer pageNo, Integer pageSize, String openId) {
		StringBuilder sql = new StringBuilder(" FROM MobileFareOrder bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND bean.openId = :openId ");
		parameters.put("openId", openId);

		sql.append(" ORDER BY bean.createTime DESC");

		return super.queryForPage(MobileFareOrder.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public MobileFareOrder findMobileFareOrder(String openId, String orderNo) {
		StringBuilder sql = new StringBuilder(" FROM MobileFareOrder bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		if (StrUtils.isNotNULL(openId)) {
			sql.append(" AND bean.openId = :openId ");
			parameters.put("openId", openId);
		}

		sql.append(" AND bean.orderNo = :orderNo ");
		parameters.put("orderNo", orderNo);

		List<MobileFareOrder> result = super.query(MobileFareOrder.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public MobileFareOrder findMobileFareOrder(String orderNo) {
		StringBuilder sql = new StringBuilder(" FROM MobileFareOrder bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.orderNo = :orderNo ");
		parameters.put("orderNo", orderNo);

		List<MobileFareOrder> result = super.query(MobileFareOrder.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public Long findTotalOrderCounts(String chiefOpenId) {
		StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM MobileFareOrder bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND bean.chiefOpenId = :chiefOpenId ");
		parameters.put("chiefOpenId", chiefOpenId);

		sql.append(" AND bean.status = :status ");
		parameters.put("status", KaMiOrder.STATUS_COMPLETE);

		List<Long> result = super.query(Long.class, sql.toString(), parameters);
		return result.isEmpty() ? 0L : result.get(0);
	}

	@Override
	public Page<MobileFareOrder> findMobileFareOrderPageByChiefOpenId(Integer pageNo, Integer pageSize,
			String chiefOpenId) {
		StringBuilder sql = new StringBuilder(" FROM MobileFareOrder bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND bean.chiefOpenId = :chiefOpenId ");
		parameters.put("chiefOpenId", chiefOpenId);

		sql.append(" ORDER BY bean.createTime DESC");

		return super.queryForPage(MobileFareOrder.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public Long findOrdersCounts(Integer status, Integer incomeGiveStatus) {
		StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM MobileFareOrder bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND bean.incomeGiveStatus = :incomeGiveStatus ");
		parameters.put("incomeGiveStatus", incomeGiveStatus);

		sql.append(" AND bean.status = :status ");
		parameters.put("status", status);

		List<Long> result = super.query(Long.class, sql.toString(), parameters);
		return result.isEmpty() ? 0L : result.get(0);
	}

	@Override
	public List<MobileFareOrder> findOrders(Integer status) {
		StringBuilder sql = new StringBuilder(" FROM MobileFareOrder bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND bean.status = :status ");
		parameters.put("status", status);

		return super.query(MobileFareOrder.class, sql.toString(), parameters);
	}

	@Override
	public Page<MobileFareOrder> findMobileFareOrderPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Date startTime, Date endTime) {
		StringBuilder sql = new StringBuilder(" FROM MobileFareOrder bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					if ("status".equals(key)) {
						String _value = (String) value;
						Integer status = Integer.valueOf(_value);
						sql.append(" AND bean.status =:status ");
						parameters.put("status", status);
					} else if ("rechargeStatus".equals(key)) {
						String _value = (String) value;
						Integer rechargeStatus = Integer.valueOf(_value);
						sql.append(" AND bean.rechargeStatus =:rechargeStatus ");
						parameters.put("rechargeStatus", rechargeStatus);
					} else {
						sql.append(" AND bean." + key + " LIKE :" + key + " ");
						parameters.put(key, "%" + value + "%");
					}
				}
			}
		}
		Date _startTime = null;
		if (startTime != null) {
			String startTimeStr = DateUtils.getDateStr(startTime, "yyyy-MM-dd") + " 00:00:00";
			_startTime = DateUtils.getDateFromDateStr(startTimeStr, "yyyy-MM-dd HH:mm:ss");

		}
		Date _endTime = null;
		if (endTime != null) {
			String endTimeStr = DateUtils.getDateStr(endTime, "yyyy-MM-dd") + " 23:59:59";
			_endTime = DateUtils.getDateFromDateStr(endTimeStr, "yyyy-MM-dd HH:mm:ss");
		}

		if (_startTime != null) {
			sql.append(" AND bean.createTime >=:startTime ");
			parameters.put("startTime", _startTime);
		}
		if (_endTime != null) {
			sql.append(" AND bean.createTime <=:endTime");
			parameters.put("endTime", _endTime);
		}

		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(MobileFareOrder.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	public List<MobileFareOrder> findOrders(Integer status, Integer settleFlag) {
		StringBuilder sql = new StringBuilder(" FROM MobileFareOrder bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<String, Object>();

		sql.append(" AND bean.status = :status ");
		parameters.put("status", status);

		sql.append(" AND bean.settleFlag = :settleFlag ");
		parameters.put("settleFlag", settleFlag);

		return super.query(MobileFareOrder.class, sql.toString(), parameters);
	}

}
